/**
 * 
 */
package net.phoenix.repository;

import java.io.IOException;
import java.io.InputStream;

import javax.jcr.AccessDeniedException;
import javax.jcr.ImportUUIDBehavior;
import javax.jcr.InvalidItemStateException;
import javax.jcr.InvalidSerializedDataException;
import javax.jcr.ItemExistsException;
import javax.jcr.NamespaceRegistry;
import javax.jcr.NoSuchWorkspaceException;
import javax.jcr.Node;
import javax.jcr.PathNotFoundException;
import javax.jcr.RepositoryException;
import javax.jcr.Session;
import javax.jcr.UnsupportedRepositoryOperationException;
import javax.jcr.Workspace;
import javax.jcr.lock.LockException;
import javax.jcr.lock.LockManager;
import javax.jcr.nodetype.ConstraintViolationException;
import javax.jcr.nodetype.NodeTypeManager;
import javax.jcr.observation.ObservationManager;
import javax.jcr.query.QueryManager;
import javax.jcr.version.Version;
import javax.jcr.version.VersionException;
import javax.jcr.version.VersionManager;
import javax.xml.parsers.ParserConfigurationException;
import javax.xml.parsers.SAXParser;
import javax.xml.parsers.SAXParserFactory;

import net.phoenix.repository.handler.CreateContentHandler;
import net.phoenix.repository.handler.RemoveContentHandler;
import net.phoenix.repository.handler.ReplaceContentHandler;
import net.phoenix.repository.handler.ThrowContentHandler;
import net.phoenix.repository.observation.BasicObservationManager;
import net.phoenix.repository.query.BasicQueryManager;

import org.xml.sax.ContentHandler;
import org.xml.sax.SAXException;
import org.xml.sax.helpers.DefaultHandler;

/**
 * @author lixf
 * 
 */
public class BasicWorkspace implements Workspace {
	private JdbcSession session;
	private String name;
	private BasicRepository repository;
	private BasicQueryManager queryManager;
	private BasicObservationManager observation;		
	private NodeTypeManager nodeTypeManager;
	public BasicWorkspace(BasicRepository repository,JdbcSession session, String name ) {
		this.repository = repository;
		this.session = session;
		this.name = name;
		this.queryManager = new BasicQueryManager(this);
		this.observation = new BasicObservationManager();
		this.nodeTypeManager = new BasicNodeTypeManager((BasicSession)session, repository.getTypeManager());
	}
	
	
	/*
	 * (non-Javadoc)
	 * 
	 * @see javax.jcr.Workspace#getSession()
	 */
	@Override
	public Session getSession() {
		return session;
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see javax.jcr.Workspace#getName()
	 */
	@Override
	public String getName() {
		return this.name;
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see javax.jcr.Workspace#copy(java.lang.String, java.lang.String)
	 */
	@Override
	public void copy(String srcAbsPath, String destAbsPath) throws ConstraintViolationException, VersionException, AccessDeniedException, PathNotFoundException, ItemExistsException, LockException,
			RepositoryException {

	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see javax.jcr.Workspace#copy(java.lang.String, java.lang.String,
	 * java.lang.String)
	 */
	@Override
	public void copy(String srcWorkspace, String srcAbsPath, String destAbsPath) throws NoSuchWorkspaceException, ConstraintViolationException, VersionException, AccessDeniedException,
			PathNotFoundException, ItemExistsException, LockException, RepositoryException {

	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see javax.jcr.Workspace#clone(java.lang.String, java.lang.String,
	 * java.lang.String, boolean)
	 */
	@Override
	public void clone(String srcWorkspace, String srcAbsPath, String destAbsPath, boolean removeExisting) throws NoSuchWorkspaceException, ConstraintViolationException, VersionException,
			AccessDeniedException, PathNotFoundException, ItemExistsException, LockException, RepositoryException {

	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see javax.jcr.Workspace#move(java.lang.String, java.lang.String)
	 */
	@Override
	public void move(String srcAbsPath, String destAbsPath) throws ConstraintViolationException, VersionException, AccessDeniedException, PathNotFoundException, ItemExistsException, LockException,
			RepositoryException {

	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see javax.jcr.Workspace#restore(javax.jcr.version.Version[], boolean)
	 */
	@Override
	public void restore(Version[] versions, boolean removeExisting) throws ItemExistsException, UnsupportedRepositoryOperationException, VersionException, LockException, InvalidItemStateException,
			RepositoryException {

	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see javax.jcr.Workspace#getLockManager()
	 */
	@Override
	public LockManager getLockManager() throws UnsupportedRepositoryOperationException, RepositoryException {
		return null;
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see javax.jcr.Workspace#getQueryManager()
	 */
	@Override
	public QueryManager getQueryManager() throws RepositoryException {
		return this.queryManager;
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see javax.jcr.Workspace#getNamespaceRegistry()
	 */
	@Override
	public NamespaceRegistry getNamespaceRegistry() throws RepositoryException {
		return this.repository.createNamespaceRegistry();
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see javax.jcr.Workspace#getNodeTypeManager()
	 */
	@Override
	public NodeTypeManager getNodeTypeManager() throws RepositoryException {
		return this.nodeTypeManager;
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see javax.jcr.Workspace#getObservationManager()
	 */
	@Override
	public ObservationManager getObservationManager() throws UnsupportedRepositoryOperationException, RepositoryException {
		return this.observation;
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see javax.jcr.Workspace#getVersionManager()
	 */
	@Override
	public VersionManager getVersionManager() throws UnsupportedRepositoryOperationException, RepositoryException {
		return null;
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see javax.jcr.Workspace#getAccessibleWorkspaceNames()
	 */
	@Override
	public String[] getAccessibleWorkspaceNames() throws RepositoryException {
		return null;
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see javax.jcr.Workspace#getImportContentHandler(java.lang.String, int)
	 */
	@Override
	public ContentHandler getImportContentHandler(String parentAbsPath, int uuidBehavior) throws PathNotFoundException, ConstraintViolationException, VersionException, LockException,
			AccessDeniedException, RepositoryException {
		Node parent = this.getSession().getNode(parentAbsPath);
		ContentHandler handler = null;
		switch (uuidBehavior) {
		case ImportUUIDBehavior.IMPORT_UUID_CREATE_NEW:
			handler = new CreateContentHandler( parent);
			break;
		case ImportUUIDBehavior.IMPORT_UUID_COLLISION_THROW:
			handler = new ThrowContentHandler( parent);
			break;
		case ImportUUIDBehavior.IMPORT_UUID_COLLISION_REPLACE_EXISTING:
			handler = new ReplaceContentHandler( parent);
			break;
		case ImportUUIDBehavior.IMPORT_UUID_COLLISION_REMOVE_EXISTING:
			handler = new RemoveContentHandler( parent);
			break;
		}
		return handler;
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see javax.jcr.Workspace#importXML(java.lang.String, java.io.InputStream,
	 * int)
	 */
	@Override
	public void importXML(String parentAbsPath, InputStream in, int uuidBehavior) throws IOException, VersionException, PathNotFoundException, ItemExistsException, ConstraintViolationException,
			InvalidSerializedDataException, LockException, AccessDeniedException, RepositoryException {
		SAXParserFactory factory = SAXParserFactory.newInstance();
		try {
			SAXParser saxParser = factory.newSAXParser();
			saxParser.parse(in, (DefaultHandler) getImportContentHandler(parentAbsPath, uuidBehavior));
		} catch (ParserConfigurationException e) {
			throw new InvalidSerializedDataException("Error in import stream to "+parentAbsPath, e);
		} catch (SAXException e) {
			throw new InvalidSerializedDataException("Error in import stream to "+parentAbsPath, e);
		}
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see javax.jcr.Workspace#createWorkspace(java.lang.String)
	 */
	@Override
	public void createWorkspace(String name) throws AccessDeniedException, UnsupportedRepositoryOperationException, RepositoryException {

	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see javax.jcr.Workspace#createWorkspace(java.lang.String,
	 * java.lang.String)
	 */
	@Override
	public void createWorkspace(String name, String srcWorkspace) throws AccessDeniedException, UnsupportedRepositoryOperationException, NoSuchWorkspaceException, RepositoryException {

	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see javax.jcr.Workspace#deleteWorkspace(java.lang.String)
	 */
	@Override
	public void deleteWorkspace(String name) throws AccessDeniedException, UnsupportedRepositoryOperationException, NoSuchWorkspaceException, RepositoryException {

	}

}
